package com.console.operations.loanmgr;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import com.common.test.GetDataFromImportFile;
import com.common.test.OperationBase;
import com.common.test.UITestBase;
import com.common.util.DBUtil;
import com.common.util.DateUtil;
import com.common.util.GetTempleteFileUtil;
import com.console.pages.LinkPage;
import com.console.pages.loanmgr.PubLoanBatchPage;

/**
 * author: wyq 
 * 2016/9/1
 * 后台-借款管理-借款批量发布-对公债权导入 操作
 */
public class PubLoanBatch extends OperationBase{

	public PubLoanBatch(UITestBase tb) {
		super(tb);
		// TODO Auto-generated constructor stub
	}
	public void pubLoanPublish() throws Exception{
		
		log.info("===start publish loan import ===");
		LinkPage lp = new LinkPage(tb);
		PubLoanBatchPage plo = new PubLoanBatchPage(tb);
		switchToDefaultContent();
		lp.loanManageLnk.click();
	    pause(2000);
	    lp.loanBatchLnk.click();
	    pause(2000);
	    plo.PublishLoanImport.click();
	    pause(2000);
	    waitForElementExist(plo.SelectFile);
	    plo.SelectFile.chooseFile(GetTempleteFileUtil.PUBLOANBATCH_TEMPLATE);
		pause(2000);
		plo.UploadFile.click();
		pause(2000);
		waitForElementExist(plo.ImportButton);
		plo.ImportButton.click();
		waitForPageContainsStr("添加债权成功");
		
	}
	public void assertloan() throws Exception{
		
		String loanTitle = GetDataFromImportFile.getDataFromCell(GetTempleteFileUtil.PUBLOANBATCH_TEMPLATE, 0);
		Map<String, String> map = new HashMap<String, String>();
		map = DBUtil.queryOneRow("select * from fiz_loan where vc_name = ?", new Object[]{loanTitle});
		String loanSeq = map.get("vc_sequence");
		System.out.println("loanSeq = " + loanSeq);
		//校验状态
		String act_status = map.get("dc_status");
		assert "00" .equals(act_status);
		//校验债权类型
		String category_code_str = GetDataFromImportFile.getDataFromCell(GetTempleteFileUtil.PUBLOANBATCH_TEMPLATE, 18);
		int category_code = DateUtil.Str2Int(category_code_str);
		System.out.println("category_code：" + category_code);
		String act_category_code_str = map.get("dc_category_code");
		int act_category_code = DateUtil.Str2Int(act_category_code_str);
		System.out.println( "导入category_code＝" + category_code + "，实际债权类型=" + act_category_code);
		assert(category_code==act_category_code);
		//校验借款金额
		String import_amount = GetDataFromImportFile.getDataFromCell(GetTempleteFileUtil.PUBLOANBATCH_TEMPLATE, 2);
		java.math.BigDecimal nb_amount = new java.math.BigDecimal(import_amount).setScale(4, BigDecimal.ROUND_HALF_UP);  
		String db_act_amount=map.get("nb_amount");
		BigDecimal act_amount = new BigDecimal(db_act_amount);
		System.out.println("导入借款金额=" + nb_amount + "，实际借款金额 =" +act_amount );
		assert nb_amount.equals(act_amount);
		//校验期数
		String nb_period_str = GetDataFromImportFile.getDataFromCell(GetTempleteFileUtil.PUBLOANBATCH_TEMPLATE, 3);
		int nb_period = DateUtil.Str2Int(nb_period_str);
		String act_nb_period_str = map.get("nb_period");
		int act_nb_period = DateUtil.Str2Int(act_nb_period_str);
		System.out.println("导入期数=" + nb_period + ",实际期数=" + act_nb_period );
		assert(nb_period == act_nb_period);
		//校验还款方式
		String import_repay = GetDataFromImportFile.getDataFromCell(GetTempleteFileUtil.PUBLOANBATCH_TEMPLATE, 14);
		String vc_repay = DBUtil.queryOneCell("select pk_id from fiz_repay where  vc_name= ?" , new Object[]{import_repay});
		String act_vc_repay = map.get("vc_repay");
		assert vc_repay.equals(act_vc_repay);
	}
	

}
